﻿using Subkismet.Services.GoogleSafeBrowsing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace UnitTests.GoogleSafeBrowsing
{


    /// <summary>
    ///This is a test class for LookupTest and is intended
    ///to contain all LookupTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LookupTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetUrls
        ///</summary>
        [TestMethod()]
        public void GetUrlsTest()
        {
            Lookup target = new Lookup();

            string url = "a.b.c.d.e.f.g/1/2/3.html?param=4";

            List<string> expected = new List<string>();
            expected.Add("a.b.c.d.e.f.g/");
            expected.Add("f.g/");
            expected.Add("e.f.g/");
            expected.Add("d.e.f.g/");
            expected.Add("c.d.e.f.g/");
            expected.Add("a.b.c.d.e.f.g/1/2/3.html?param=4");
            expected.Add("a.b.c.d.e.f.g/1/2/3.html");
            expected.Add("a.b.c.d.e.f.g/1/");
            expected.Add("a.b.c.d.e.f.g/1/2/");

            List<string> actual;

            actual = target.GetUrls(url);

            TestGeneric<string>(actual, expected);
        }


        /// <summary>
        ///A test for GetExactHostname
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetExactHostnameTest()
        {
            Lookup_Accessor target = new Lookup_Accessor();

            string url = "a.b.c.d.e.f.g/1.html";

            string expected = "a.b.c.d.e.f.g/";
            string actual;

            actual = target.GetExactHostname(url);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetFourHostNames
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetFourHostNamesTest()
        {
            Lookup_Accessor target = new Lookup_Accessor();

            string hostname = "4.d2.eyh.f-i.gk/";

            List<string> expected = new List<string>();
            expected.Add("f-i.gk/");
            expected.Add("eyh.f-i.gk/");
            expected.Add("d2.eyh.f-i.gk/");
            expected.Add("4.d2.eyh.f-i.gk/");

            List<string> actual;

            actual = target.GetFourHostNames(hostname);

            TestGeneric<string>(actual, expected);
        }

        /// <summary>
        ///A test for GetExactPath
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetExactPathTest()
        {
            Lookup_Accessor target = new Lookup_Accessor();

            string url = "a.b.c/1/2.html";

            string expected = "a.b.c/1/2.html";
            string actual;

            actual = target.GetExactPath(url);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetExactPathWithoutParameters
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetExactPathWithoutParametersTest()
        {
            Lookup_Accessor target = new Lookup_Accessor();

            string url = "a.b.c/1/2.html?param=1";

            string expected = "a.b.c/1/2.html";
            string actual;

            actual = target.GetExactPathWithoutParameters(url);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetFourPaths
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetFourPathsTest()
        {
            Lookup_Accessor target = new Lookup_Accessor();

            string hostname = "a.b.c/";

            string urlWithoutParameters = "a.b.c/1/2-p/abc/45/5.html";

            List<string> expected = new List<string>();
            expected.Add("a.b.c/1/");
            expected.Add("a.b.c/1/2-p/");
            expected.Add("a.b.c/1/2-p/abc/");
            expected.Add("a.b.c/1/2-p/abc/45/");

            List<string> actual;

            actual = target.GetFourPaths(hostname, urlWithoutParameters);

            TestGeneric<string>(actual, expected);
        }

        /// <summary>
        ///A test for RemoveDuplicates
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void RemoveDuplicatesTest()
        {
            Lookup_Accessor target = new Lookup_Accessor();

            List<string> results = new List<string>();
            results.Add("http://google.com");
            results.Add("http://blogml.org");
            results.Add("http://subkismet.com");
            results.Add("http://google.com");
            results.Add("http://google.com");
            results.Add("http://blogml.org");


            List<string> expected = new List<string>();
            expected.Add("http://google.com");
            expected.Add("http://blogml.org");
            expected.Add("http://subkismet.com");

            List<string> actual;

            actual = target.RemoveDuplicates(results);

            TestGeneric<string>(actual, expected);
        }

        #region Helper methods

        /// <summary>
        /// Helper method to test generic lists.
        /// </summary>
        /// <typeparam name="T">The type.</typeparam>
        /// <param name="actual">Actual list of T.</param>
        /// <param name="expected">Expected list of T.</param>
        private void TestGeneric<T>(List<T> actual, List<T> expected)
        {
            IEnumerator<T> actualEnumerator = actual.GetEnumerator();

            for (int i = 0; i < expected.Count; i++)
            {
                Assert.IsTrue(actualEnumerator.MoveNext());
                Assert.IsTrue(actualEnumerator.Current.Equals(expected[i]));
            }

            Assert.IsFalse(actualEnumerator.MoveNext());
        }

        #endregion
    }
}
